package com.dianrui.firemenmonitor.util;

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.util.Log;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class OkHttpUtils {
    private Handler handler;
    private static OkHttpClient okHttpClient = new OkHttpClient();
    static OkHttpUtils okHttpUtils;

    public OkHttpUtils() {
        handler = new Handler(Looper.getMainLooper());

    }


    /**
     * 单例模式
     */
    //获取单利
    public static OkHttpUtils getInstance(){

        if (okHttpUtils==null) {
            synchronized (OkHttpUtils.class)
            {
                if (okHttpUtils==null)
                {
                    okHttpUtils=new OkHttpUtils();
                }
            }
        }
        return okHttpUtils;

    }


    public void postRequests(final String url, final String requestparams, final XCallBack callback) {
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        Request request = new Request.Builder().url(url).post(RequestBody.create(mediaType, requestparams)).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e("TAG", "请求失败=" + e.getMessage());
                onSuccessResponse(e.getMessage(), callback);
                call.cancel();
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    ResponseBody responseBody = response.body();
                    String date = responseBody.string();
                    onSuccessResponse(date, callback);
                    Log.e("服务器返回数据---->", "====================================网络请求[" + "],请求日志如下===================================" +
                            "\n\n请求地址:" + url +
                            "\n\n请求数据响应代码:" + response.code() +
                            "\n\n需要传递的参数:" + requestparams +
                            "\n\n服务器返回的数据>" + date);
                }else {
                    Log.e("TAG", "请求失败=" +response.toString());
                    onSuccessResponse(response.message(), callback);
                    call.cancel();
                }
            }
        });

    }


    public void postWithMemberToken(final String url, final String requestparams, final XCallBack callback) {
        String token = SharedUtil.getToken();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        Request request = new Request.Builder().url(url).post(RequestBody.create(mediaType, requestparams)).addHeader("token", token).build();
        Log.e("当前token是---->", token);
        okHttpClient.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e("TAG", "请求失败=" + e.getMessage());
                onSuccessResponse(call.toString(), callback);
                call.cancel();
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if (response!=null && response.isSuccessful()) {
                    ResponseBody responseBody = response.body();
                    String date = responseBody.string();
                    onSuccessResponse(date, callback);
                    Log.e("服务器返回数据---->", "====================================网络请求[" + "],请求日志如下===================================" +
                            "\n\n请求地址:" + url +
                            "\n\n请求数据响应代码:" + response.code() +
                            "\n\n需要传递的参数:" + requestparams +
                            "\n\n服务器返回的数据>" + date);
                }else {
                    Log.e("TAG", "请求失败=" +response.toString());
                    onSuccessResponse(response.message(), callback);
                    call.cancel();
                }
            }
        });


    }

    private void onSuccessResponse(final String result, final XCallBack callBack) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (callBack != null) {
                    callBack.onResponse(result);
                }
            }
        });
    }

    public interface XCallBack {
        void onResponse(String result);
    }
}
